﻿
package com.example.getmysignaturehashcode;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.PrintStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class MainActivity extends Activity
{
	static final int GOOGLE_ENG_SIGNATURE_SHA1 = -1263674583;
	static final String GOOGLE_ENG_SIGNATURE_SHA256 = "c13594a1465c1c7ec47e0477be849d9a46fc3100c1d7ed3456bd9679716a728f";
	static final String[][] GoogleKeyInfos = { { "verity key", "ae959405f84c9475a7907fec2d24093387ee08b61f940077c6641adcb1253701" },
			{ "testkey", "c88c9048f6d0fe9d8561926240f2ccc1982e24721150929350384659aa54aef6" },
			{ "shared key", "651085ea6b1e04a01eb705f558db7684d871fe971cce7f890f60813330f54d47" },
			{ "platform key", "c13594a1465c1c7ec47e0477be849d9a46fc3100c1d7ed3456bd9679716a728f" },
			{ "media key", "9b0702ef8db95f863a7d496bbe6d6aa415b99bc84551fea48114cc8925e78d38" } };
	
	private String getSHA256(String paramString)
	{
		try
		{
			MessageDigest localMessageDigest = MessageDigest.getInstance("SHA-256");
			localMessageDigest.update(paramString.getBytes());
			byte[] arrayOfByte = localMessageDigest.digest();
			StringBuffer localStringBuffer = new StringBuffer();
			for (int i = 0;; i++)
			{
				if (i >= arrayOfByte.length) return localStringBuffer.toString();
				localStringBuffer.append(Integer.toString(256 + (0xFF & arrayOfByte[i]), 16).substring(1));
			}
		}
		catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
		{
			localNoSuchAlgorithmException.printStackTrace();
		}
		return null;
	}
	
	public void onCreate(Bundle paramBundle)
	{
		super.onCreate(paramBundle);
		setContentView(R.layout.activity_main);
		final TextView localTextView1 = (TextView) findViewById(R.id.mainTextView);
		final TextView localTextView2 = (TextView) findViewById(R.id.subTextView);
		final EditText localEditText = (EditText) findViewById(R.id.Edit);
		Button localButton = (Button) findViewById(R.id.btn);
		try
		{
			Signature[] arrayOfSignature = getPackageManager().getPackageInfo("android", 64).signatures;
			if (-1263674583 == arrayOfSignature[0].hashCode())
			{
				System.out.println("This is Google engineer binary:" + arrayOfSignature[0].hashCode());
				localTextView1.setText("This is Google engineer binary. Plz get hashcode from user binary");
			}
			localButton.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View paramAnonymousView)
				{
					String str1 = localEditText.getText().toString();
					try
					{
						Signature[] arrayOfSignature = getPackageManager().getPackageInfo(str1, 64).signatures;
						
						int m = 0;
						while (true)
						{
							// break;
							Signature localSignature = arrayOfSignature[0];	// -640762507
							int k = localSignature.hashCode();
							System.out.println("short key : " + k);
							String str2 = MainActivity.this.getSHA256(localSignature.toCharsString());
							System.out.println("long key : " + str2);
							
							if (m >= MainActivity.GoogleKeyInfos.length)
							{
								localTextView1.setText("-short key : " + k + " \n-long key: " + str2);
								break;
							}
							
							if (str2.equals(MainActivity.GoogleKeyInfos[m][1]))
							{
								localTextView2.setText("The signing key of this apk is a google " + MainActivity.GoogleKeyInfos[m][0]
										+ ". Strongly recommend using a private key.");
								continue;
							}
							
							m++;
						}
					}
					catch (PackageManager.NameNotFoundException localNameNotFoundException)
					{
						localNameNotFoundException.printStackTrace();
						localTextView1.setText("Your Package is not installed");
						return;
					}
				}
			});
			return;
		}
		catch (PackageManager.NameNotFoundException localNameNotFoundException)
		{
			while (true)
			{
				localNameNotFoundException.printStackTrace();
				localTextView1.setText("android package error");
			}
		}
	}
	
	public boolean onCreateOptionsMenu(Menu paramMenu)
	{
		getMenuInflater().inflate(R.menu.activity_main, paramMenu);
		return true;
	}
}
